package com.coolgor.xcode;

import java.io.*;
import java.util.List;

public class StackEntity {

    public static void run(String basePath, List<String[]> context) throws IOException {

        String outputPath = basePath + XcoderConfig.OutputPath + "/entity";
        String tableName = null;
        String className = null;
        String keyColumn = null;
        String keyParam = null;
        for (int i = 0; i < context.size(); i++) {
            switch (context.get(i)[0].toLowerCase()){
                case "table":
                    tableName = context.get(i)[1];
                    className = StringUtil.firstUpper(context.get(i)[2]);
                    break;
                case "key":
                    keyColumn = context.get(i)[1];
                    keyParam = context.get(i)[2];
                    break;
            }
        }

        StringBuffer param = new StringBuffer();
        StringBuffer getsetter = new StringBuffer();

        File file =new File(outputPath);
        if (!file.exists()) file.mkdirs();
        file = new File(outputPath+"/"+className+".java");
        if (!file.exists()) file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        bw.write("public class "+className+" {\n");
        bw.newLine();
        for (int i = 1; i < context.size(); i++) {
            switch(context.get(i)[0].toLowerCase()){
                case "key","long":
                    param.append("\tprivate Long "+context.get(i)[2]+";\n");
                    getsetter.append("\tpublic Long get"+ StringUtil.firstUpper(context.get(i)[2]) +"() {\n");
                    getsetter.append("\t\treturn "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    getsetter.append("\tpublic void set"+ StringUtil.firstUpper(context.get(i)[2]) +"(Long "+context.get(i)[2]+") {\n");
                    getsetter.append("\t\tthis."+context.get(i)[2]+" = "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    break;
                case "int","integer":
                    param.append("\tprivate Integer "+context.get(i)[2]+";\n");
                    getsetter.append("\tpublic Integer get"+ StringUtil.firstUpper(context.get(i)[2]) +"() {\n");
                    getsetter.append("\t\treturn "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    getsetter.append("\tpublic void set"+ StringUtil.firstUpper(context.get(i)[2]) +"(Integer "+context.get(i)[2]+") {\n");
                    getsetter.append("\t\tthis."+context.get(i)[2]+" = "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    break;
                case "string":
                    param.append("\tprivate String "+context.get(i)[2]+";\n");
                    getsetter.append("\tpublic String get"+ StringUtil.firstUpper(context.get(i)[2]) +"() {\n");
                    getsetter.append("\t\treturn "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    getsetter.append("\tpublic void set"+ StringUtil.firstUpper(context.get(i)[2]) +"(String "+context.get(i)[2]+") {\n");
                    getsetter.append("\t\tthis."+context.get(i)[2]+" = "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    break;
                case "date":
                    param.append("\t@JsonFormat(pattern = \"yyyy-MM-dd\", timezone = \"GMT+8\")\n");
                    param.append("\tprivate Date "+context.get(i)[2]+";\n");
                    getsetter.append("\tpublic Date get"+ StringUtil.firstUpper(context.get(i)[2]) +"() {\n");
                    getsetter.append("\t\treturn "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    getsetter.append("\tpublic void set"+ StringUtil.firstUpper(context.get(i)[2]) +"(Date "+context.get(i)[2]+") {\n");
                    getsetter.append("\t\tthis."+context.get(i)[2]+" = "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    break;
                case "datetime":
                    param.append("\t@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")\n");
                    param.append("\tprivate Date "+context.get(i)[2]+";\n");
                    getsetter.append("\tpublic Date get"+ StringUtil.firstUpper(context.get(i)[2]) +"() {\n");
                    getsetter.append("\t\treturn "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    getsetter.append("\tpublic void set"+ StringUtil.firstUpper(context.get(i)[2]) +"(Date "+context.get(i)[2]+") {\n");
                    getsetter.append("\t\tthis."+context.get(i)[2]+" = "+context.get(i)[2]+";\n");
                    getsetter.append("\t}\n");
                    break;
            }
        }
        bw.write(param.toString());
        bw.newLine();
        bw.write(getsetter.toString());
        bw.newLine();
        bw.write("}");
        bw.close();
    }
}
